layui.use(['layer','from','table','jquery'],function () {
    let layer=layui.layer;
    let from=layui.from;
    let table=layui.table;
    let $=layui.layer;

    //  表格
    table.render({
        elem: '#empTable'
        ,url: '/emp/page' //数据接口
        ,page: true //开启分页
        ,cols: [[ //表头
            {type: 'numbers', title: '序号'}
            ,{field: 'empNO', title: '员工编号'}
            ,{field: 'ename', title: '员工姓名'}
            ,{field: 'job', title: '员工岗位'}
            ,{field: 'mgr', title: '员工上级编号'}
            ,{field: 'hiredate', title: '入职日期'}
            ,{field: 'sal', title: '工资'}
            ,{field: 'comm', title: '奖金'}
            ,{title: '操作',toolbar:'#empTool'}
        ]]
    });
    form.on('submit(searchFilter)',function(data){
        //让表格进行一次渲染，需要把查询条件带上，由属性where 携带 ,值是json格式
        tabInstance.reload({
            where:data.field
        });
    });

    let addIndex ;  //弹出层的ID
    //添加页面
    $(".add_btn").click(function(){
        $.get('/dept/list',function(res){
            console.log(res);
            res.data.forEach(function(opt){
                $("#dept").append('<option value="'+opt.deptNo+'">'+opt.dname+'</option>');
            });
            form.render('select', 'addFormFilter');

        });
        // 上级信息  ajax
        $.get('/emp/mgrInfo',function(res){
            console.log(res);
            res.data.forEach(function(opt){
                $("#mgr").append('<option value="'+opt.empNo+'">'+opt.ename+'</option>');
            });

            form.render('select', 'addFormFilter');
        });
        //页面层
        addIndex = layer.open({
            type: 1,
            title:'添加员工',
            anim: 2,
            skin: 'layui-layer-molv',
            area: ['650px', '425px'],
            content: $("#addFormInfo")
        });
    });


    //添加功能实现
    form.on('submit(addFormSubmit)',function(obj){
        console.log(obj.field);
        $.post('/emp/save',obj.field,function(res){
            layer.close(addIndex);
            tabInstance.reload();
            if(res.code===200){
                layer.msg(res.msg, {time: 2000, icon:6});
            }else{
                layer.msg(res.msg, {time: 2000, icon:5});
            }
        });
    });


    table.on('tool(empTable)', function(obj){
        let data = obj.data;
        let layEvent = obj.event;

        if(layEvent === 'del'){
            layer.confirm('真的删除【'+data.empno+'】吗？', function(index){
            obj.del();
            layer.close(index);
            });
        } else if(layEvent === 'edit'){
            layer.msg('编辑操作');
        }
    });

})